<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractNum']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractName']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractType']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractMoney']" disabled>
                <span slot="addonAfter">元</span>
              </a-input>
            </a-form-item>
          </a-col>
          <!--          <a-col :span='12'>-->
          <!--            <a-form-item label='回款条件' :labelCol='labelCol' :wrapperCol='wrapperCol'>-->
          <!--              <a-input v-decorator="['returnCondition']" disabled></a-input>-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <a-col :span="12">
            <a-form-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['customerName']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="计划回款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['expectMoney']" disabled>
                <span slot="addonAfter">元</span>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="实际回款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['actualMoney']" disabled>
                <span slot="addonAfter">元</span>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开票名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['billCompany', validatorRules.billCompany]" placeholder="请输入开票单位"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开票金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['invoicedAmount', validatorRules.billCompany]" placeholder="请输入开票金额">
                <span slot="addonAfter">元</span>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开票税号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['taxNum', validatorRules.taxNum]" placeholder="请输入开票税号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="发票性质" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['billingCharacter', validatorRules.billingCharacter]"
                :trigger-change="true"
                dictCode="icm_contract_sale_apply_invoice_billing_character"
                placeholder="请选择发票性质"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开户行及账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['bankDeposit']" placeholder="请输入开户行及账号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="税率" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['taxRate', validatorRules.taxRate]"
                :trigger-change="true"
                dictCode="invoice_rate"
                placeholder="请选择税率"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['address']" placeholder="请输入地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productName', validatorRules.productName]" placeholder="请输入产品名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['tel']" placeholder="请输入电话"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm" type="primary">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ContractSaleApplyInvoiceForm',
  components: {},
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false,
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      validatorRules: {
        billingCharacter: {
          rules: [{ required: true, message: '请选择发票性质!' }],
        },
        taxNum: {
          rules: [{ required: true, message: '请输入税号!' }],
        },
        billCompany: {
          rules: [{ required: true, message: '请输入开票单位!' }],
        },
        invoicedAmount: {
          rules: [{ required: true, message: '请输入开票金额!' }],
        },
        taxRate: {
          rules: [
            { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' },
            { required: true, message: '请输入税率!' },
          ],
        },
        productName: {
          rules: [{ required: true, message: '请输入产品名称!' }],
        },
        tel: {
          rules: [{ pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!' }],
        },
      },
      url: {
        add: '/contract/contractSaleApplyInvoice/add',
        edit: '/contract/contractSaleApplyInvoice/edit',
        queryById: '/contract/contractSaleApplyInvoice/queryById',
      },
      fields: [
        'billCompany',
        'billingCharacter',
        'taxRate',
        'productName',
        'taxNum',
        'bankDeposit',
        'address',
        'tel',
        'paymentCollectionId',
        'bpmStatus',
        'contractName',
        'opportunityNum',
        'opportunityName',
        'customerName',
        'returnCondition',
        'contractNum',
        'contractMoney',
        'contractType',
        'expectMoney',
        'actualMoney',
        'invoicedAmount',
      ],
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    },
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      if (record.contractType == '1') {
        record.contractType = '技贸合同'
      } else if (record.contractType == '2') {
        record.contractType = '双软合同'
      } else {
        record.contractType = '其他'
      }
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    },
  },
}
</script>